package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GameGUI implements EntryPoint {
	RootPanel rootPanel;
	VerticalPanel mainPanel = new VerticalPanel();
	VerticalPanel vplPlayers = new VerticalPanel();
	VerticalPanel selectPanel = new VerticalPanel();
	HorizontalPanel selectXPanel = new HorizontalPanel();
	HorizontalPanel selectOPanel = new HorizontalPanel();
	Label lblTitle = new Label("Noughts and Crosses");
	Label lblTitleBuildSelect = new Label("Noughts and Crosses");
	ListBox playerXimage = new ListBox();
	ListBox playerOimage = new ListBox();
	TextArea playerXname = new TextArea();
	TextArea playerOname = new TextArea();
	Label playerXinstructions = new Label("Please give a name and picture for X");
	Label playerOinstructions = new Label("Please give a name and picture for O");
	Image selectXimage = new Image("images/bull.png");
	Image selectOimage = new Image("images/bull.png");
	Button selectButton = new Button("Press to play");
	Button resetButton = new Button("Change players?");
	private  String Xpic = "images/bull.png";
	private  String Opic = "images/bull.png";
	private String Xname = "X";
	private String Oname = "O";
	private static final String aWidth = "120px";
	private static final String aHeight = "100px";
	private String []listNames = {"Bull","Bus","Cat","Mia","Croc","Ship","Tiger"};
	private String [] listUrls = {"images/bull.png", "images/bus.png", "images/cat.png", "images/mia.png","images/croc.png",
			"images/ship.png","images/tiger.png"};
	private boolean gameOver = false;
	private int [] displayWinner = {10,10,10};
	Image image = new Image(Xpic);
	FlexTable fteMainTable = new FlexTable();
	FlexTable ftePlayer = new FlexTable();
	Label lblPlayerName = new Label(Xname + "'s turn.  Place an X in a box.");
	private  String PlayX = Xname + "'s turn.  Place an X in a box.";
	private  String PlayO = Oname + "'s turn.  Place an O in a box.";
	Button btnTL = new Button(".");
	Button btnTM = new Button(".");
	Button btnTR = new Button(".");
	Button btnML = new Button(".");
	Button btnMM = new Button(".");
	Button btnMR = new Button(".");
	Button btnBL = new Button(".");
	Button btnBM = new Button(".");
	Button btnBR = new Button(".");
	Label lblWinner = new Label("No winner yet");
	Button btnreset = new Button("Play again?");	
	GameMechanics game = new GameMechanics();	
	
	@Override
	public void onModuleLoad() {
		rootPanel = RootPanel.get("wrapper");
		rootPanel.getElement().getStyle().setPosition(Position.RELATIVE);
		
		buildViewGame();
		buildViewSelect();	
		
	}
	
	private void buildViewSelect(){
		rootPanel.clear();
		rootPanel.add(selectPanel);
		selectPanel.setSize("400px", "130px");
		lblTitleBuildSelect.setStyleName("lblMainDisplay");
		selectPanel.add(lblTitleBuildSelect);
		playerXimage.clear();
		for(int i = 0; i<listNames.length; i++){
			playerXimage.addItem(listNames [i]);
		}
		playerXimage.setVisibleItemCount(1);
		playerXimage.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				selectXimage.setUrl(listUrls[playerXimage.getSelectedIndex()]);
				Xpic = listUrls[playerXimage.getSelectedIndex()];
			}});
		playerXname.setSize("100px", "20px");
		playerXname.setText(Xname);
		selectXimage.setSize("70px", "70px");
		playerXinstructions.setStyleName("selectPadding");
		selectXPanel.add(playerXimage);
		selectXPanel.add(selectXimage);
		selectXPanel.add(playerXname);
		selectXPanel.add(playerXinstructions);
		selectXPanel.setStyleName("selectPadding");
		selectXPanel.setSize("400px", "80px");
		selectPanel.add(selectXPanel);
		
		playerOimage.clear();
		for(int i = 0; i<listNames.length; i++){
			playerOimage.addItem(listNames [i]);
		}
		playerOimage.setVisibleItemCount(1);
		playerOimage.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				selectOimage.setUrl(listUrls[playerOimage.getSelectedIndex()]);
				Opic = listUrls[playerOimage.getSelectedIndex()];
			}});
		playerOname.setSize("100px", "20px");
		playerOname.setText(Oname);
		selectOimage.setSize("70px", "70px");
		playerOinstructions.setStyleName("selectPadding");
		selectOPanel.add(playerOimage);
		selectOPanel.add(selectOimage);
		selectOPanel.add(playerOname);
		selectOPanel.add(playerOinstructions);
		selectOPanel.setStyleName("selectPadding");
		selectOPanel.setSize("400px", "80px");
		selectPanel.add(selectOPanel);
		
		
		selectButton.setSize(aWidth, aHeight);
		selectButton.setStyleName("selectButton");
		selectButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				Xname = playerXname.getText();
				if(Xname.length()>10){
					Xname = Xname.substring(0, 9);
				}
				Oname = playerOname.getText();
				if(Oname.length()>10){
					Oname = Oname.substring(0, 9);
				}
				lblPlayerName.setText(Xname + "'s turn.  Place an X in a box.");
				PlayX = Xname + "'s turn.  Place an X in a box.";
				PlayO = Oname + "'s turn.  Place an O in a box.";
				rootPanel.clear();
				rootPanel.add(mainPanel, 10, 10);
				btnreset.setVisible(false);
				resetButton.setVisible(false);
				game.resetData();
				btnTL.setText(".");
				btnTM.setText(".");
				btnTR.setText(".");
				btnML.setText(".");
				btnMM.setText(".");
				btnMR.setText(".");
				btnBL.setText(".");
				btnBM.setText(".");
				btnBR.setText(".");	
				lblWinner.setText("No winner yet");
				lblPlayerName.setText(PlayX);
				image.setUrl(Xpic);
				gameOver=false;
				displayWinner[0]=10;
				displayWinner[1]=10;
				displayWinner[2]=10;
				displayWinner(displayWinner);
			}});
		selectPanel.add(selectButton);
		}
	
	private void buildViewGame(){
		rootPanel.clear();
		rootPanel.add(mainPanel, 10, 10);
		mainPanel.setSize("278px", "280px");	
		lblTitle.setStyleName("lblMainDisplay");
		mainPanel.add(lblTitle);			
		lblTitle.setWidth("380px");
		mainPanel.add(vplPlayers);
		vplPlayers.setWidth("280px");	
		vplPlayers.add(ftePlayer);
		ftePlayer.setWidth("370px");
		ftePlayer.setWidget(0, 0, image);
		image.setUrl(Xpic);
		image.setSize("101px", "85px");
		lblPlayerName.setStyleName("lblMidSize");
		ftePlayer.setWidget(0,1, lblPlayerName);
			
		mainPanel.add(fteMainTable);
		fteMainTable.setWidth("276px");
		btnTL.setStyleName("btnMainDisplay");
		
		btnTL.addClickHandler(new ClickHandler(){
		@Override
		public void onClick(ClickEvent event) {
			if(!gameOver){
			if(game.getCurrentPlay()==1 && game.getCurrentElement(0)==0){
				btnTL.setText("X");
			}
			if(game.getCurrentPlay()==2 && game.getCurrentElement(0)==0){
				btnTL.setText("O");
			}
			buttonAction(0);
			}
		}});
				
		fteMainTable.setWidget(0, 0, btnTL);
		btnTL.setSize(aWidth, aHeight);
		btnTM.setStyleName("btnMainDisplay");
		btnTM.addClickHandler( new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(1)==0){
					btnTM.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(1)==0){
					btnTM.setText("O");
				}
				buttonAction(1);
				}
			}});
		fteMainTable.setWidget(0, 1, btnTM);
		btnTM.setSize(aWidth, aHeight);
		btnTR.setStyleName("btnMainDisplay");
		btnTR.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(2)==0){
					btnTR.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(2)==0){
					btnTR.setText("O");
				}
				buttonAction(2);
				}
			}});
		fteMainTable.setWidget(0, 2, btnTR);
		btnTR.setSize(aWidth, aHeight);
		btnML.setStyleName("btnMainDisplay");
		btnML.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(3)==0){
					btnML.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(3)==0){
					btnML.setText("O");
				}
				buttonAction(3);
				}
			}});
		fteMainTable.setWidget(1, 0, btnML);
		btnML.setSize(aWidth, aHeight);
		btnMM.setStyleName("btnMainDisplay");
		btnMM.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(4)==0){
					btnMM.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(4)==0){
					btnMM.setText("O");
				}
				buttonAction(4);
				}
			}});
		fteMainTable.setWidget(1, 1, btnMM);
		btnMM.setSize(aWidth, aHeight);
		btnMR.setStyleName("btnMainDisplay");
		btnMR.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(5)==0){
					btnMR.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(5)==0){
					btnMR.setText("O");
				}
				buttonAction(5);
				}
			}});
		fteMainTable.setWidget(1, 2, btnMR);
		btnMR.setSize(aWidth, aHeight);
		btnBL.setStyleName("btnMainDisplay");
		btnBL.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(6)==0){
					btnBL.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(6)==0){
					btnBL.setText("O");
				}
				buttonAction(6);
				}
			}});
		fteMainTable.setWidget(2, 0, btnBL);
		btnBL.setSize(aWidth, aHeight);
		btnBM.setStyleName("btnMainDisplay");
		btnBM.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(7)==0){
					btnBM.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(7)==0){
					btnBM.setText("O");
				}
				buttonAction(7);
				}
			}});
		fteMainTable.setWidget(2, 1, btnBM);
		btnBM.setSize(aWidth, aHeight);
		btnBR.setStyleName("btnMainDisplay");
		btnBR.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!gameOver){
				if(game.getCurrentPlay()==1 && game.getCurrentElement(8)==0){
					btnBR.setText("X");
				}
				if(game.getCurrentPlay()==2 && game.getCurrentElement(8)==0){
					btnBR.setText("O");
				}
				buttonAction(8);
				}
			}});
		fteMainTable.setWidget(2, 2, btnBR);
		btnBR.setSize(aWidth, aHeight);
		lblWinner.setStyleName("lblMidSize");
		lblWinner.setWidth("380px");
			
		mainPanel.add(lblWinner);
		btnreset.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				game.resetData();
				btnTL.setText(".");
				btnTM.setText(".");
				btnTR.setText(".");
				btnML.setText(".");
				btnMM.setText(".");
				btnMR.setText(".");
				btnBL.setText(".");
				btnBM.setText(".");
				btnBR.setText(".");	
				btnreset.setVisible(false);
				lblWinner.setText("No winner yet");
				lblPlayerName.setText(PlayX);
				image.setUrl(Xpic);
				gameOver=false;
				displayWinner[0]=10;
				displayWinner[1]=10;
				displayWinner[2]=10;
				displayWinner(displayWinner);
				
			}});
		
		resetButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				rootPanel.clear();
				rootPanel.add(selectPanel);			
			}});
		btnreset.setVisible(false);
		btnreset.setSize(aWidth, aHeight);
		btnreset.setStyleName("btnReset");
		fteMainTable.setWidget(3,1,btnreset);
		resetButton.setVisible(false);
		fteMainTable.setWidget(3,2, resetButton);
	}//close buildViewGame
	
	private void displayWinner(int[] winner) {
		if(winner[0]==10){
			btnTL.removeStyleName("btnWinner");
			btnTM.removeStyleName("btnWinner");
			btnTR.removeStyleName("btnWinner");
			btnML.removeStyleName("btnWinner");
			btnMM.removeStyleName("btnWinner");
			btnMR.removeStyleName("btnWinner");
			btnBL.removeStyleName("btnWinner");
			btnBM.removeStyleName("btnWinner");
			btnBR.removeStyleName("btnWinner");
			btnTL.setStyleName("btnMainDisplay");
			btnTM.setStyleName("btnMainDisplay");
			btnTR.setStyleName("btnMainDisplay");
			btnML.setStyleName("btnMainDisplay");
			btnMM.setStyleName("btnMainDisplay");
			btnMR.setStyleName("btnMainDisplay");
			btnBL.setStyleName("btnMainDisplay");
			btnBM.setStyleName("btnMainDisplay");
			btnBR.setStyleName("btnMainDisplay");
			return;
		}
		for(int i=0; i< winner.length; i++){
			if(winner[i]==0){
				btnTL.setStyleName("btnWinner");
			}
			if(winner[i]==1){
				btnTM.setStyleName("btnWinner");
			}
			if(winner[i]==2){
				btnTR.setStyleName("btnWinner");
			}
			if(winner[i]==3){
				btnML.setStyleName("btnWinner");
			}
			if(winner[i]==4){
				btnMM.setStyleName("btnWinner");
			}
			if(winner[i]==5){
				btnMR.setStyleName("btnWinner");
			}
			if(winner[i]==6){
				btnBL.setStyleName("btnWinner");
			}
			if(winner[i]==7){
				btnBM.setStyleName("btnWinner");
			}
			if(winner[i]==8){
				btnBR.setStyleName("btnWinner");
			}		
		}	
	}

	private void gameOver(){
		btnreset.setVisible(true);
		resetButton.setVisible(true);
		gameOver=true;
		displayWinner = game.getWinnerLocations();
		displayWinner(displayWinner);
		if(game.checkForWinnerX()){
			lblWinner.setText("The winner is " + Xname + ".");
		}
		if (game.checkForWinnerO()){
			lblWinner.setText("The winner is " + Oname + ".");
		}
		if (!game.checkForWinnerO() && !game.checkForWinnerX()){
			lblWinner.setText("There is not a winner...");
		}
		
	}
	
	private void buttonAction(int i){
		if(game.getCurrentElement(i) == 0 && !gameOver){
			game.setButton(i);
			if(game.checkWinner()){
				gameOver();
			}else{
				game.swap();
				if(game.getCurrentPlay()==1){
					lblPlayerName.setText(PlayX);
					image.setUrl(Xpic);
				}else{
					lblPlayerName.setText(PlayO);
					image.setUrl(Opic);
				}				
			}
		}	
	}
}
